package com.vitabuybuy.tv.ui.mine.talent.usercenter

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.core.os.bundleOf
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ActivityWithdrawAccountBinding
import com.vitabuybuy.tv.dto.BankCardListDto
import com.library.common.base.BaseActivity
import com.library.common.base.MessageModel
import com.vitabuybuy.tv.ui.mine.talent.usercenter.viewmodel.TalentWalletViewModel
import kotlinx.android.synthetic.main.activity_withdraw_account.*

class TalentWithdrawAccountActivity :
    BaseActivity<TalentWalletViewModel, ActivityWithdrawAccountBinding>() {
    override val isUseBaseTitleBar: Boolean
        get() = true
    override val layoutId: Int
        get() = R.layout.activity_withdraw_account

    private var requestCode :Int?= -1
    private var pageNumber = 1


    override fun onGetBundle(bundle: Bundle?) {
        requestCode = bundle?.getInt("requestCode")
    }

    override fun initView(savedInstanceState: Bundle?) {
        setTitle(getString(R.string.withdraw_account))
        mBinding.mViewModel = viewModel
        mIvRight.apply {
            setImageResource(R.drawable.icon_navbar_add)
            visibility = View.VISIBLE
            setOnClickListener {
                TalentAddWithdrawAccountActivity.openActivity(mContext)
            }
        }
    }


    override fun onResume() {
        super.onResume()
        viewModel.getBankCardList(pageNumber)
    }

    override fun eventHandling() {
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                viewModel.getBankCardList(pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                viewModel.getBankCardList(pageNumber)
            }
        })

    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                viewModel.getBankCardList(pageNumber)
            }
            1 -> {
                if (requestCode == 0x11) {
                    val intent = Intent()
                    intent.putExtras(bundleOf(Pair("bankDto", msg.obj as BankCardListDto)))
                    finishResult(intent)
                }
            }
            2->{
                refresh.onLoad(msg.arg1)
            }
        }
    }

    companion object {
        fun openActivity(mContext: BaseActivity<*, *>, requestCode: Int) {
            mContext.startForResult(
                bundleOf(Pair("requestCode", requestCode)),
                requestCode,
                TalentWithdrawAccountActivity::class.java
            )
        }
    }
}
